creation of user accounts from input file.

Hello,

I found some code from the Kitchen scripts and reuse some of the code mainly the interface, I am trying to create a script to create user based on an input file and assign them accordingly or override them and assign different user groups to the user(s). I was wondering what is the best way to parse a csv file to get the groups list after each individual user syntax: doors.user, group, group, etc. Regular expressions aren't my think so I am not sure what the best way to approach this. Any help would be appreciated. Anyone that has a script that does this and is willing to share would also be appreciated.

Thank you,
Jim

//////////////////////////////////////////////
//  Data
 
const string NOGROUPS = "(No Groups)"
const string NOUSERS  = "(No Users)"
 
Module currMod = current Module
 
string dummyList[] = { }
 
const Regexp isPatt = regexp2 ".*[,]"
 
 
//////////////////////////////////////////////
//  Dialogue boxes
 
DB  createUsersDB  = null
DBE lstGroups      = null
DBE lstUsers       = null
DBE btnCreate      = null
DBE btnAllGroups   = null
DBE btnAllUsers    = null
DBE btnClearGroups = null
DBE btnClearUsers  = null
DBE btnMatchUser   = null
DBE fldPattUser    = null
DBE btnMatchGroup  = null
DBE fldPattGroup   = null
DBE selectedGroups = null
DBE selectedUsers  = null
DBE scrFilename       = null
 
//////////////////////////////////////////////
//  Regular functions
string addEscapesForRegexp(string txt) {
 
    if ( txt == "" ) return ""
 
    Buffer b = create()
    int i = 0
    while ( matches("(\"|\\\\n|\\\\t)", txt[i:]) ) {
 
        b += txt[i:i+start(0)-1]
 
        if ( txt[i+start(0):i+end(0)] == "\\n" )
        {
            b += "\n"
        } else
        if ( txt[i+start(0):i+end(0)] == "\\t" )
        {
            b += "\t"
        } else
        if ( txt[i+start(0):i+end(0)] == "\"" )
        {
            b += "\\\""
        } else
        {
            b += txt[i+start(0):i+end(0)]
        }
        
        i += (end 0)+1
    }
    
    b += txt[i:]
    
    string res = b ""
    delete(b)
 
    return res
}
 
 
Regexp regexpOf(string patt)
{ // make string into a regular expression, and catch any errors
 
    noError()
    Regexp isPatt = regexp patt
    string errmsg = lastError()
    if ( errmsg != "" )
    {
        if ( matches("<[^>]*> *", errmsg) ) errmsg = errmsg[end(0)+1:]
        ack errmsg
        return null
    }
    
    return isPatt
}
 
 
//////////////////////////////////////////////
//  Auxiliary functions
 
 
int fillrGroupList(DBE dbeList) 
{ // fill DBE list with details of views
 
    realize parentOf dbeList
 
    // empty list
        empty(dbeList)
 
    int i = 0
        Group group 
        for group in groupList do {
                string gName = group.name
        insert(dbeList, i, gName)
        i++
        } 
    
    return noElems dbeList
}
 
 
int fillUserLists(DBE dbeList) 
{ // fill DBE list with details of modules
 
    realize parentOf dbeList
 
    // empty list
        empty(dbeList) 
    
    return noElems dbeList
}
 
 
//////////////////////////////////////////////
//  Call backs
 
void doSelect(DBE dbe)
{
    int tv = noElems(lstGroups)
    int nv = 0
    string s
    for s in lstGroups do nv++
 
    s = get(lstGroups, 0)
    if ( s == NOGROUPS ) 
    {
        set(lstGroups, 0, false)
        tv = 0
        nv = 0
    }
    
    int tm = noElems(lstUsers)
    int nm = 0
    for s in lstUsers do nm++
    
    s = get(lstUsers, 0)
    if ( s == NOUSERS ) 
    {
        set(lstUsers, 0, false)
        tm = 0
        nm = 0
    }
    
    if ( nv == 0 || nm == 0 ) inactive btnCreate
    else active btnCreate
    
    set(selectedUsers, nm "/" tm "")
    set(selectedGroups,  nv "/" tv "")
}
 
 
void inputFile(DBE dbe)
{  
        empty(lstUsers) 
    
        int i = 0
        int cntline = 0
        Stream input = read(get(scrFilename))
        string str
        while (true) {
                input >> str // read a line at a time
                cntline++
                if (null str and end input) break 
                if (null str)   continue
                
                if (isPatt str) {
                        insert(lstUsers, i, str)
                        i++
                }
                else {
                        warningBox("#error {line#:"cntline"}: '"str"'")
                }
 
        } 
 
        close input 
        
        if ( i == 0 ) 
        { // nothing to copy
                inactive btnCreate
                insert(lstUsers, 0, NOUSERS)
                warningBox(NOUSERS)
        }
        
        doSelect(lstUsers)
}
 
 
void removeUserFromList (DBE dbeList)
{
        int i = 0
        bool msg = true
        
        //Loop through the module list.
        for (i = noElems (dbeList); i >= 0; i--)
        {    
                //If the item is selected then remove it from list to process.
                if (selected (dbeList, i))
                {    
                        delete (dbeList, i)
                        msg = false
                }    
        }
        
        if(msg){warningBox "No item(s) selected!"}
        
        doSelect(dbeList)
}
 
 
void UserCreation(DB db)
{
        string entry = null
        int i = 0
    for entry in lstUsers do
    {
                if (isPatt entry) 
                {
                        string sUserName = entry[start(0):end(0)-1] 
                        if (existsUser(sUserName) == false && existsGroup(sUserName) == false) 
                        {
                                bool bCreateCompleted = false
                                // query user
                                string sErrorMsg = createPasswordDialog(dbExplorer, bCreateCompleted)
                                // check status
                                if (sErrorMsg == null && bCreateCompleted == true) 
                                {
                                        // add new user
                                        if (addUser(sUserName, null) == null) 
                                        {
                                                string sgroup = null
                                                for sgroup in lstGroups do 
                                                {
                                                        if(existsGroup(sgroup))
                                                        {
                                                                Group g = find(sgroup)
                                                                if(g == null)
                                                                {
                                                                  // warn user
                                                                  warningBox("Failed to find group.\n")
                                                                }
                                                                else
                                                                {
                                                                        User cUser = find(sUserName)
                                                                        if(cUser == null)
                                                                        {
                                                                          // warn user
                                                                          warningBox("Failed to find user. '"sUserName".\n")
                                                                        }
                                                                        else
                                                                        {
                                                                          // add user group
                                                                          addMember(g, cUser)
                                                                        }
                                                                }
                                                        }
                                                        else
                                                        {
                                                                // warn user
                                                                warningBox("Group doesn't exist '"sgroup"'.\n")
                                                        }
                                                }
                                                // save new user list
                                                if (saveDirectory() == null) 
                                                {
                                                   // refresh
                                                   if (loadDirectory() == null)
                                                   {
                                                         // inform user
                                                         infoBox("User '"sUserName"'was added successfully.\n")
                                                   } 
                                                   else 
                                                   {
                                                         // warn user
                                                         warningBox("Failed to load user list.\n")
                                                   }
                                                } 
                                                else 
                                                {
                                                        // warn user
                                                        warningBox("Failed to save user list.\n")
                                                }
                                        } 
                                        else 
                                        {
                                                // warn user
                                                warningBox("Failed to add user '"sUserName"'.\n")
                                        }
                                }
                                else 
                                {
                                        // warn user
                                        warningBox(sErrorMsg)
                                }
                        }
                        else
                        {
                                // warn user
                                warningBox("The name '"sUserName"' already exists as either a DOORS User or Group.\n")
                        }
                }
    }
        removeUserFromList(lstUsers)
}
 
 
void doSelectAllGroups(DBE dbe)
{ // select all elements of the list
    int i
    for i in 0:noElems(lstGroups)-1 do
    {
        set(lstGroups, i, true)
    }
    
    doSelect(lstGroups)
}
 
 
void doClearUsers(DBE dbe)
{ // select all elements of the list
    int i
    for i in 0:noElems(lstGroups)-1 do
    {
        set(lstGroups, i, false)
    }
    
    doSelect(lstGroups)
}
 
 
void doSelectAllUsers(DBE dbe)
{ // select all elements of the list
    int i
    for i in 0:noElems(lstUsers)-1 do
    {
        set(lstUsers, i, true)
    }
 
    doSelect(lstUsers)
}
 
 
void doClearUserList(DBE dbe)
{ // select all elements of the list
    int i
    for i in 0:noElems(lstUsers)-1 do
    {
        set(lstUsers, i, false)
    }
 
    doSelect(lstUsers)
}
 
 
void doMatchUser(DBE dbe)
{
    string patt   = get(fldPattUser)
    Regexp isPatt = regexpOf(patt)
 
    if ( null isPatt ) return
 
    int i
    for i in 0:noElems(lstUsers)-1 do
    {
        string thisMod = get(lstUsers, i)
        set(lstUsers, i, ( isPatt thisMod ))
    }
 
    doSelect(lstUsers)
}
 
 
void doMatchGroup(DBE dbe)
{
    string patt   = get(fldPattGroup)
    Regexp isPatt = regexpOf(patt)
 
    if ( null isPatt ) return
        
    int i
    for i in 0:noElems(lstGroups)-1 do
    {
        string thisMod = get(lstGroups, i)
        set(lstGroups, i, ( isPatt thisMod ))
    }
 
    doSelect(lstGroups)
}
 
 
void doNothing(DB db)
{ // do nothing
}
 
 
//////////////////////////////////////////////
//  Main program
 
 
if ( !null currMod )
{
    ack "Please run at the project level."
    halt
}
 
createUsersDB = create("Create New Users")
 
lstGroups               = multiList(createUsersDB, "User Group(s):", 250, 20, dummyList)
lstUsers                = multiList(createUsersDB, "UserList for Creation:",     250, 20, dummyList)
 
btnAllGroups            = button(createUsersDB, "Select all", doSelectAllGroups)
beside(createUsersDB)
btnClearGroups          = button(createUsersDB, "Clear", doClearUsers)
selectedGroups          = field(createUsersDB, "# selected:", "", 4, true)
 
btnAllUsers             = button(createUsersDB, "Select all", doSelectAllUsers)
btnClearUsers           = button(createUsersDB, "Clear", doClearUserList)
selectedUsers           = field(createUsersDB, "# selected:", "", 4, true)
left(createUsersDB)
btnMatchGroup           = button(createUsersDB, "Matching", doMatchGroup)
beside(createUsersDB)
fldPattGroup            = field(createUsersDB, "", ".*", 22)
btnMatchUser        = button(createUsersDB, "Matching", doMatchUser)
beside(createUsersDB)
fldPattUser             = field(createUsersDB, "", ".*", 22)
left(createUsersDB)
scrFilename                     = fileName(createUsersDB, "C:\\", "*.txt", "source")
 
btnCreate               = apply(createUsersDB, "Create", UserCreation)
 
lstGroups->"right"->"unattached"
lstUsers->"left"->"flush"->lstGroups
lstUsers->"top"->"aligned"->lstGroups
lstUsers->"right"->"form"
 
btnAllUsers->"left"->"aligned"->lstUsers
btnMatchUser->"left"->"aligned"->btnAllUsers
 
inactive(btnCreate)
 
set(lstGroups, doSelect, doSelect)
set(lstUsers,  doSelect, doSelect)
set(scrFilename, inputFile)
 
if ( fillrGroupList(lstGroups) == 0 ) 
{ // nothing to copy
    inactive btnCreate
    insert(lstGroups, 0, NOGROUPS)
        warningBox(NOGROUPS)
}
 
if ( fillUserLists(lstUsers) == 0 ) 
{ // nothing to copy
    inactive btnCreate
    insert(lstUsers, 0, NOUSERS)
        warningBox(NOUSERS)
}
 
doSelect(lstUsers)
 
show(createUsersDB)

SystemAdmin - Wed Aug 01 13:48:05 EDT 2012

Re: creation of user accounts from input file.
Mathias Mamsch - Fri Aug 03 07:00:27 EDT 2012

I wrote this CSV parse function a long time ago, but I am not sure if it works 100% ... Maybe it gives you a head start. Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Attachments

attachment_14871786_parseCSV.dxl